<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="客戶編碼" prop="customerCode">
        <el-input
          v-model="queryParams.customerCode"
          placeholder="請輸入客戶編碼"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客戶名稱" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="請輸入客戶名稱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="聯繫人" prop="contacts">
        <el-input
          v-model="queryParams.contacts"
          placeholder="請輸入聯繫人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="區號" prop="diallingCode">
        <el-input
          v-model="queryParams.diallingCode"
          placeholder="請輸入區號"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="客戶類型" prop="customerType">
        <el-select
          v-model="queryParams.customerType"
          placeholder="請選擇客戶類型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.customer_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="聯繫電話" prop="telPhone">
        <el-input
          v-model="queryParams.telPhone"
          placeholder="請輸入聯繫電話"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="證件類型" prop="cardType">
        <el-select
          v-model="queryParams.cardType"
          placeholder="請選擇證件類型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.cart_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="證件號" prop="cardNo">
        <el-input
          v-model="queryParams.cardNo"
          placeholder="請輸入證件號"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="郵箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="請輸入郵箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性別" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="請選擇性別" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="所屬行業" prop="industry">
        <el-select
          v-model="queryParams.industry"
          placeholder="請選擇所屬行業"
          clearable
        >
          <el-option
            v-for="dict in dict.type.industry_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="手機號" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="請輸入手機號"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司地址" prop="companyAddr">
        <el-input
          v-model="queryParams.companyAddr"
          placeholder="請輸入公司地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客戶標籤" prop="customerTag">
        <el-select
          v-model="queryParams.customerTag"
          placeholder="請選擇客戶標籤"
          clearable
        >
          <el-option
            v-for="dict in dict.type.customer_tag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客戶等級" prop="customerLevel">
        <el-select
          v-model="queryParams.customerLevel"
          placeholder="請選擇客戶等級"
          clearable
        >
          <el-option
            v-for="dict in dict.type.customer_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="客戶來源" prop="customerSource">
        <el-select
          v-model="queryParams.customerSource"
          placeholder="請選擇客戶來源"
          clearable
        >
          <el-option
            v-for="dict in dict.type.customer_source"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['biz:customer:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:customer:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:customer:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:customer:export']"
          >導出</el-button
        >
      </el-col> -->
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="customerList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column  show-overflow-tooltip label="主鍵" align="center" prop="id" /> -->
      <el-table-column
        show-overflow-tooltip
        label="客戶編碼"
        width="180"
        align="center"
        prop="customerCode"
      />
      <el-table-column
        show-overflow-tooltip
        label="客戶名稱"
        width="100"
        align="center"
        prop="customerName"
      />
      <el-table-column
        show-overflow-tooltip
        label="聯繫人"
        align="center"
        prop="contacts"
      />
      <el-table-column
        show-overflow-tooltip
        label="區號"
        align="center"
        prop="diallingCode"
      />
      <el-table-column
        show-overflow-tooltip
        label="客戶類型"
        align="center"
        prop="customerType"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.customer_type"
            :value="scope.row.customerType"
          />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="聯繫電話"
        align="center"
        prop="telPhone"
      />
      <el-table-column
        show-overflow-tooltip
        label="證件類型"
        align="center"
        prop="cardType"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.cart_type"
            :value="scope.row.cardType"
          />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="證件號"
        align="center"
        prop="cardNo"
      />
      <el-table-column
        show-overflow-tooltip
        width="180"
        label="郵箱"
        align="center"
        prop="email"
      />
      <el-table-column
        show-overflow-tooltip
        label="性別"
        align="center"
        prop="sex"
      >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="所屬行業"
        align="center"
        prop="industry"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.industry_type"
            :value="scope.row.industry"
          />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="手機號"
        align="center"
        prop="mobile"
      />
      <el-table-column
        show-overflow-tooltip
        label="公司地址"
        align="center"
        prop="companyAddr"
      />
      <el-table-column
        show-overflow-tooltip
        label="客戶標籤"
        align="center"
        prop="customerTag"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.customer_tag"
            :value="scope.row.customerTag"
          />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="客戶等級"
        align="center"
        prop="customerLevel"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.customer_level"
            :value="scope.row.customerLevel"
          />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="客戶來源"
        align="center"
        prop="customerSource"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.customer_source"
            :value="scope.row.customerSource"
          />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="備註"
        align="center"
        prop="remark"
      />
      <el-table-column
        label="操作"
        width="140"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:customer:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            >詳情</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:customer:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客戶对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客戶名稱" prop="customerName">
          <el-input v-model="form.customerName" placeholder="請輸入客戶名稱" />
        </el-form-item>
        <el-form-item label="聯繫人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="請輸入聯繫人" />
        </el-form-item>
        <el-form-item label="區號" prop="diallingCode">
          <el-input v-model="form.diallingCode" placeholder="請輸入區號" />
        </el-form-item>
        <el-form-item label="客戶類型" prop="customerType">
          <el-select v-model="form.customerType" placeholder="請選擇客戶類型">
            <el-option
              v-for="dict in dict.type.customer_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="聯繫電話" prop="telPhone">
          <el-input v-model="form.telPhone" placeholder="請輸入聯繫電話" />
        </el-form-item>
        <el-form-item label="證件類型" prop="cardType">
          <el-select v-model="form.cardType" placeholder="請選擇證件類型">
            <el-option
              v-for="dict in dict.type.cart_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="證件號" prop="cardNo">
          <el-input v-model="form.cardNo" placeholder="請輸入證件號" />
        </el-form-item>
        <el-form-item label="郵箱" prop="email">
          <el-input v-model="form.email" placeholder="請輸入郵箱" />
        </el-form-item>
        <el-form-item label="傳真" prop="facsimile">
          <el-input v-model="form.facsimile" placeholder="請輸入傳真" />
        </el-form-item>
        <el-form-item label="聯繫地址" prop="address">
          <el-input v-model="form.address" placeholder="請輸入聯繫地址" />
        </el-form-item>
        <el-form-item label="性別" prop="sex">
          <el-select v-model="form.sex" placeholder="請選擇性別">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年齡" prop="age">
          <el-input v-model="form.age" placeholder="請輸入年齡" />
        </el-form-item>
        <el-form-item label="所屬行業" prop="industry">
          <el-select v-model="form.industry" placeholder="請選擇所屬行業">
            <el-option
              v-for="dict in dict.type.industry_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手機號" prop="mobile">
          <el-input v-model="form.mobile" placeholder="請輸入手機號" />
        </el-form-item>
        <el-form-item label="公司地址" prop="companyAddr">
          <el-input v-model="form.companyAddr" placeholder="請輸入公司地址" />
        </el-form-item>
        <el-form-item label="客戶標籤" prop="customerTag">
          <el-select v-model="form.customerTag" placeholder="請選擇客戶標籤">
            <el-option
              v-for="dict in dict.type.customer_tag"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客戶等級" prop="customerLevel">
          <el-select v-model="form.customerLevel" placeholder="請選擇客戶等級">
            <el-option
              v-for="dict in dict.type.customer_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客戶來源" prop="customerSource">
          <el-select v-model="form.customerSource" placeholder="請選擇客戶來源">
            <el-option
              v-for="dict in dict.type.customer_source"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="備註" prop="remark">
          <el-input v-model="form.remark" placeholder="請輸入備註" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">確 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCustomer,
  getCustomer,
  delCustomer,
  addCustomer,
  updateCustomer,
} from "@/api/biz/customer";

export default {
  name: "Customer",
  dicts: [
    "customer_type",
    "customer_tag",
    "customer_source",
    "industry_type",
    "cart_type",
    "sys_user_sex",
    "customer_level",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中數組
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 顯示搜索条件
      showSearch: true,
      // 总条數
      total: 0,
      // 客戶表格數据
      customerList: [],
      // 弹出层標题
      title: "",
      // 是否顯示弹出层
      open: false,
      // 查询參數
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerCode: null,
        customerName: null,
        contacts: null,
        diallingCode: null,
        customerType: null,
        telPhone: null,
        cardType: null,
        cardNo: null,
        email: null,
        sex: null,
        industry: null,
        mobile: null,
        companyAddr: null,
        customerTag: null,
        customerLevel: null,
        customerSource: null,
        orderByColumn: "update_time",
        isAsc: "desc",
      },
      // 表单參數
      form: {},
      // 表单校验
      rules: {
        customerCode: [
          { required: true, message: "客戶編碼不能為空", trigger: "blur" },
        ],
        customerName: [
          { required: true, message: "客戶名稱不能為空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客戶列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then((response) => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerCode: null,
        customerName: null,
        contacts: null,
        diallingCode: null,
        customerType: null,
        customerTypeName: null,
        telPhone: null,
        cardType: null,
        cardTypeName: null,
        cardNo: null,
        email: null,
        facsimile: null,
        address: null,
        sex: null,
        age: null,
        industry: null,
        industryName: null,
        mobile: null,
        companyAddr: null,
        customerTag: null,
        customerLevel: null,
        customerSource: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中數据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.$router.push({ path: "/customer/index/add" });
      // this.open = true;
      this.title = "添加客戶";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCustomer(id).then((response) => {
        this.form = response.data;
        // this.open = true;
        this.$router.push({
          path: "/customer/index/edit",
          query: response.data,
        });
        this.title = "修改客戶";
      });
    },
    handleDetail(row){
      const id = row.id || this.ids;
      this.$router.push({ path: "/customer/index/detail", query:{id:id} });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomer(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否確認删除客戶編號為"' + ids + '"的數据项？')
        .then(function () {
          return delCustomer(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 導出按钮操作 */
    handleExport() {
      this.download(
        "biz/customer/export",
        {
          ...this.queryParams,
        },
        `customer_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
